package com.loong.web.gp4j.view.user;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.loong.web.gp4j.bean.param.user.UserDetailParam;
import com.loong.web.gp4j.bean.param.user.UserPwdParam;
import com.loong.web.gp4j.bean.schema.code.Codes;
import com.loong.web.gp4j.bean.schema.user.Auths;
import com.loong.web.gp4j.service.code.CodeService;
import com.loong.web.gp4j.service.message.UserMessageService;
import com.loong.web.gp4j.service.user.UserDetailService;
import com.loong.web.gp4j.service.user.UserLoginService;
import com.loong.web.gp4j.service.user.UserService;
import com.loong.web.suppert.collection.param.PageParam;
import com.loong.web.suppert.collection.valid.ErrorList;
import com.loong.web.suppert.collection.valid.ValidAdapter;
import com.loong.web.suppert.collection.vo.ResultVo;
import com.loong.web.suppert.intercept.auth.AuthType;
import com.loong.web.suppert.intercept.statistic.Statistic;

/**
 * 用户资源
 *
 * @author 张成轩
 */
@Controller
@RequestMapping("user")
public class UserViewMapping {

	@Resource
	private UserService userService;
	@Resource
	private UserDetailService userDetailService;
	@Resource
	private UserLoginService userLoginService;
	@Resource
	private CodeService codeService;
	@Resource
	private UserMessageService userMessageService;

	/**
	 * 用户设置主页
	 * 
	 * @return 视图
	 */
	@RequestMapping(value = "", method = RequestMethod.GET)
	@AuthType(Auths.USER)
	public ModelAndView user() {

		return new ModelAndView("redirect:/user/info/");
	}

	/**
	 * 用户详情页面
	 * 
	 * @param request 请求
	 * @param param 参数
	 * @return 视图
	 */
	@RequestMapping(value = "info", method = RequestMethod.GET)
	@AuthType(Auths.USER)
	@Statistic("用户详情")
	public ModelAndView info(HttpServletRequest request, UserDetailParam param) {

		userDetailService.setUserDetail(request, param);

		ModelAndView model = new ModelAndView("gp4j/user/info");
		model.addObject("sexs", codeService.getCodeMap(Codes.SEX));
		return model;
	}

	/**
	 * 用户详情提交
	 * 
	 * @param request 请求
	 * @param param 参数
	 * @param valid 校验器
	 * @return 视图
	 */
	@RequestMapping(value = "info", method = RequestMethod.POST)
	@AuthType(Auths.USER)
	public ModelAndView infoSubmit(HttpServletRequest request, @Valid UserDetailParam param, BindingResult valid) {

		ModelAndView model = new ModelAndView("gp4j/user/info");
		model.addObject("sexs", codeService.getCodeMap(Codes.SEX));

		if (!valid.hasErrors()) {
			// 错误集合
			ErrorList errorList = new ErrorList();
			// 调用服务层
			boolean succ = userDetailService.saveUserDetail(request, param, errorList);
			// 判断是否包含错误信息
			if (!succ)
				ValidAdapter.convert(param, errorList, valid);
			model.addObject("succ", succ);
		}
		return model;
	}

	/**
	 * 修改密码页面
	 * 
	 * @param request 请求
	 * @param param 参数
	 * @return 视图
	 */
	@RequestMapping(value = "pwd", method = RequestMethod.GET)
	@AuthType(Auths.USER)
	@Statistic("修改密码")
	public ModelAndView pwd(HttpServletRequest request, UserPwdParam param) {

		return new ModelAndView("gp4j/user/pwd");
	}

	/**
	 * 修改密码页面提交
	 * 
	 * @param request 请求
	 * @param param 参数
	 * @param valid 校验器
	 * @return 视图
	 */
	@RequestMapping(value = "pwd", method = RequestMethod.POST)
	@AuthType(Auths.USER)
	public ModelAndView pwdSubmit(HttpServletRequest request, @Valid UserPwdParam param, BindingResult valid) {

		ModelAndView model = new ModelAndView("gp4j/user/pwd");
		if (!valid.hasErrors()) {
			// 错误集合
			ErrorList errorList = new ErrorList();
			// 调用服务层
			boolean succ = userService.editPassword(request, param, errorList);
			// 判断是否包含错误信息
			if (!succ)
				ValidAdapter.convert(param, errorList, valid);
			else {
				param.setNpwd(null);
				param.setPwd(null);
			}
			model.addObject("succ", succ);
		}
		return model;
	}

	/**
	 * 重置用户密码
	 * 
	 * @param request 请求
	 * @return 返回对象
	 */
	@RequestMapping(value = "pwd/repwd", method = RequestMethod.POST)
	@AuthType(Auths.USER)
	@ResponseBody
	public ResultVo<?> repwd(HttpServletRequest request) {

		return userService.rePassword(request);
	}

	/**
	 * 登录日志页面
	 * 
	 * @param request 请求
	 * @param page 分页
	 * @return 视图
	 */
	@RequestMapping(value = "log", method = RequestMethod.GET)
	@AuthType(Auths.USER)
	@Statistic("登录日志")
	public ModelAndView logs(HttpServletRequest request, PageParam page) {

		ModelAndView model = new ModelAndView("gp4j/user/log");
		model.addObject("userLogins", userLoginService.queryUserLogin(request, page));
		return model;
	}

	/**
	 * 用户消息页面
	 * 
	 * @param request 请求
	 * @param read 是否已读
	 * @param page 分页
	 * @return 视图
	 */
	@RequestMapping(value = "message", method = RequestMethod.GET)
	@AuthType(Auths.USER)
	@Statistic("用户消息")
	public ModelAndView message(HttpServletRequest request, Boolean read, PageParam page) {

		ModelAndView model = new ModelAndView("gp4j/user/message");
		model.addObject("messages", userMessageService.queryUserMessage(request, read, page));
		return model;
	}

	/**
	 * 标记消息已读
	 * 
	 * @param request 请求
	 * @param id 用户消息id
	 * @return 返回对象
	 */
	@RequestMapping(value = "message/read", method = RequestMethod.POST)
	@AuthType(Auths.USER)
	@ResponseBody
	public ResultVo<?> readMessage(HttpServletRequest request, Integer id) {

		return userMessageService.readMessage(request, id);
	}
}
